﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace InstaMe
{
    public class MyItemsControl : ItemsControl
    {
        Rect[] mRect;
        private int mRow;
        private int mCol;
        private Rect WRONG_RECT = new Rect(0, 0, 0, 0);
        public object SelectedItem;
        public int SelectedIndex;

        public MyItemsControl()
        {
            RefreshCommand = new RefreshViewCommand(this.UpdateMyItemsControl);
        }

        public ICommand RefreshCommand
        {
            get;
            private set;
        }

        private void UpdateMyItemsControl()
        {
            for (int iter = 0; iter < this.Items.Count; iter++)
            {
                ContentPresenter iViewObj = this.ItemContainerGenerator.ContainerFromIndex(iter) as ContentPresenter;
                if (iViewObj != null)
                {
                    GridWrapper iWrapper = FrameworkElementUtils.FindElement<GridWrapper>(iViewObj, "");
                    if (iWrapper != null)
                    {
                        iWrapper.RaiseEvent();
                    }
                }
            }
        }

        private void CreateRect(int Count)
        {
            mCol = 1;
            if (Count == 1)
            {
                mCol = 1;
            }
            else if (Count == 2 || Count == 4)
            {
                mCol = 2;
            }
            else
            {
                mCol = 3;
            }
            double itemWidth = this.Width / mCol;
            mRow = Count / mCol;
            if (Count % mCol != 0)
            {
                mRow++;
            }
            mRect = new Rect[mRow * mCol];
            for (int i = 0; i < mRow; i++)
                for (int j = 0; j < mCol; j++)
                {
                    int index = i * mCol + j;
                    if (index >= this.Items.Count)
                    {
                        break;
                    }
                    Rect rect = new Rect();
                    rect.X = j * itemWidth;
                    rect.Y = i * itemWidth;
                    rect.Width = itemWidth;
                    rect.Height = itemWidth;
                    mRect[i * mCol + j] = rect;
                }
        }

        private bool isInRect(Point point, Rect rect)
        {
            if ((point.X >= rect.Left && point.X <= rect.Right) && (point.Y >= rect.Top && point.Y <= rect.Bottom))
            {
                return true;
            }
            return false;
        }

        private int GetIndexEmoticon(Point point)
        {
            for (int i = 0; i < mRow; i++)
                for (int j = 0; j < mCol; j++)
                {
                    int index = i * mCol + j;
                    if (index >= this.Items.Count)
                    {
                        break;
                    }
                    if (isInRect(point, mRect[index]))
                    {
                        return index;
                    }
                }
            return -1;
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            Point iPoint = e.GetPosition(this);
            SelectedIndex = GetIndexEmoticon(iPoint);
            base.OnMouseLeftButtonDown(e);
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            Point iPoint = e.GetPosition(this);
            int index = GetIndexEmoticon(iPoint);
            if (index != -1 && SelectedIndex == index)
            {
                SelectedItem = this.Items[index];
            }
            base.OnMouseLeftButtonUp(e);
        }

        protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (this.Items.Count > 0)
            {
                CreateRect(this.Items.Count);
            }
            base.OnItemsChanged(e);
        }
    }
}
